﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DocManagerORM.ORM;
using System.Web;
using System.Web.Security;
using DocManagerControls.CLASSI;

namespace DocManagerControls.Controls
{
    /// <summary>
    /// Logica di interazione per UtentiControl.xaml
    /// </summary>
    public partial class UtentiControl : BaseCusControl
    {
        QueryUsers m_QueryUsers = new QueryUsers();

        public UtentiControl()
        {
            InitializeComponent();
            CaricaDatiVari();
        }

        private void CaricaDatiVari()
        {
            var utentiView = m_QueryUsers.getVwMembershipSimple(false).ToArray();
            var ruoli = m_QueryUsers.getRUOLI().ToArray();

            cmbRuoli.DisplayMemberPath = "RoleName";
            cmbRuoli.SelectedValuePath = "RoleId";
            cmbRuoli.ItemsSource = ruoli;

            lstUtenti.DisplayMemberPath = "UserName";
            lstUtenti.SelectedValuePath = "UserId";
            lstUtenti.ItemsSource = utentiView;

            utentiView = null;
            ruoli = null;


             String adPath = "LDAP://fideuram.bancafideuram.it"; //Fully-qualified Domain Name
             ActiveDir acDir = new ActiveDir();
             acDir.LDAPPathFull = adPath;

              try
              {
                  if (true == acDir.IsAuthenticated(Environment.UserDomainName, Environment.UserName, "diego$1"))
                  {
                      var groups = acDir.GetGroups();
                      var users = acDir.GetUsers(Environment.UserName, "diego$1");
                  }



                  var de = ActiveDir.GetDirectoryEntry(adPath, System.DirectoryServices.AuthenticationTypes.Secure);
                  var esiste = ActiveDir.UserExists("Z800631", adPath);

              }
              catch (Exception ex)
              { 
                
              }


        }//fM




        protected void getUtentiActiveDir()
        { 
            //lstUtentiActDir.ItemsSource = ;
        
        
        }//fM


        private void btnCreaUtente_Click(object sender, RoutedEventArgs e)
        {           
            MembershipCreateStatus stato;
                       

            if(string.IsNullOrEmpty(txtUserName.Text))
            {
                messaggia("Manca l' username "); return ;
            }

             if(string.IsNullOrEmpty(txtPassword.Text))
            {
                messaggia("Manca la password "); return ;
            }

            if( ! txtPassword.Text.Equals(txtPassword2.Text))
            {
                messaggia("Le password sono diverse "); return ;
            }

            if (string.IsNullOrEmpty( txtEmail.Text))
            {
                messaggia("Manca l'email ");return;
            }


              var utente = Membership.CreateUser(txtUserName.Text, txtPassword.Text,txtEmail.Text,
                  null,null,true,out stato);
              

            txtMessaggi.Text = "Stato creazione :  "+  Enum.GetName(typeof(MembershipCreateStatus),stato) ; 

        }//fM


    }//fC
}
